<template>
    <div class="message">
      <div class="title">
        <div class="back-wrapper">
          <back></back>
        </div>
        消息中心
      </div>
      <div class="refresh-wrapper" v-show="isRefresh">
        <refresh></refresh>
      </div>
      <loading v-show="isLoading"></loading>
      <div class="content" ref="content">
        <div class="content-container">
          <div class="empty" v-show="isEmpty">
            <p class="text">您还有有消息哦</p>
          </div>
          <div class="line-block-item" v-for="item in msgList">
            <h1 class="name" >
              <span class="tag">{{item.label}}</span>{{item.title}}
            </h1>
            <p class="time">{{item.pushTime}}</p>
            <p class="msg">{{item.content}}</p>
          </div>
          <endLine :text="'我是有底线的'" v-show="endFlag&&msgList"></endLine>
        </div>
      </div>
    </div>
</template>

<script>
  import back from '../../../components/back/back'
  import BScroll from 'better-scroll'
  import {getStorage} from "../../../common/js/localStorage";
  import refresh from '../../../components/refresh/refresh'
  import endLine from '../../../components/endLine/endLine'
  import loading from '../../../components/loading/loading'
  import global from '../../../common/js/global'
  import {Toast} from 'mint-ui'
    export default {
        name: "message",
        data(){
            return {
              rows:10,
              offset:10,
              endFlag:false,
              msgList:[],
              msgListLen:10,
              isRefresh:false,
              isEmpty:true,
              isLoading:false
            }
        },
        mounted() {
          this._getMessageList()
          this.$nextTick(()=>{
            if(!this.scroll){
              // return
              let scroll = new BScroll(this.$refs['content'],{
                click:true,
                pullUpLoad:{
                threshold:-50
              },
              pullDownRefresh: {
                threshold: 40,
                  stop: 30
              }})

              scroll.on('pullingDown', () => {
                this.isRefresh = true
                setTimeout(()=>{
                  window.history.go(0)
                  scroll.finishPullDown();
                },600)
              });
              let _this = this
              scroll.on('pullingUp',()=>{
                if(!_this.endFlag){
                  _this.rows+=10;
                  _this._getMessageList()
                }
                if(this.msgListLen < this.msgList.length){

                  this.msgListLen = this.msgList.length
                  scroll.refresh()
                }

                scroll.finishPullUp()

              })
            }else {
              this.scroll.refresh();
            }
          })
        },
        methods:{
          _getMessageList(){
            this.isLoading = true
            this.$axios(
              {
                method:'post',
                headers:{
                  "Content-Type":"application/json",
                  "MOBILE-DEVICE":'PSD_H5_1_MPS',
                  "JSESSIONID":getStorage('userInfo').JSESSIONID
                },
                url:global.$host+'/member/myMessageList',
                data:{
                  offset:0,
                  rows:this.rows
                }
              }
            ).then((res)=>{
              if(res.status===global.ERR_OK){
                if(res.data.code===0){
                  this.isLoading = false
                  let data = res.data.data
                  this.endFlag = data.endFlag
                  this.msgList = data.msgList
                  if(!this.msgList){
                    this.isEmpty = true
                  }else {
                    this.isEmpty = false
                  }
                }else if(res.data.code===10) {
                  this.$router.push('/login')
                }
              }else {
                Toast(res.data.errorMsg)
              }

            })
          }
        },
        computed:{
        },
        components:{
          'back':back,
          'refresh':refresh,
          'endLine':endLine,
          'loading':loading
        }
    }
</script>

<style scoped lang="stylus">
  .message
    position absolute
    height 100%
    width 100%
    background #101011
    user-select none
    overflow hidden
    .title
      position relative
      padding 10px 0
      height 22px
      line-height 22px
      text-align center
      font-size 16px
      color #ffffff
      .back-wrapper
        position absolute
        left 0
        top 10px
        width 36px
        height 22px
    .content
      height calc(100% - 42px)
      overflow hidden
      .content-container
        .empty
          position absolute
          top 80px
          left calc(50% - 80px)
          height 20px
          padding-top 160px
          width 160px
          background url("icon_saw_empty@2x.png")no-repeat center top/124px 124px
          .text
            height 20px
            line-height 20px
            font-size 12px
            color #666666

        .line-block-item
          margin-bottom 10px
          text-align left
          background #171718
          padding 25px 15px 23px 15px
          .name
            height 20px
            line-height 20px
            color #ffffff
            font-size 14px
            .tag
              height 14px
              background #F7C20F
              display inline-block
              font-size 10px
              line-height 14px
              color #171718
              padding 0 3px
              border-radius 2px
              margin-right 4px
          .time
            padding-top 9px
            height 17px
            line-height 17px
            color #8B929E
            font-size 12px
          .msg
            color #ffffff
            font-size 12px
            line-height 18px
            padding-top 11px

</style>
